package com.ganmiao.server.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ganmiao.exception.DefaultBusinessException;
import com.ganmiao.server.sys.entity.SysUserAccount;
import com.ganmiao.server.sys.repository.SysUserAccountRepository;
import com.ganmiao.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户账号
 */
@Service
public class SysUserAccountService {

    @Autowired
    private SysUserAccountRepository sysUserAccountRepository;

    /**
     * 账号是否存在
     *
     * @return
     */
    public boolean accountIsExist(String account, String sysCode) {

        //查询条件
        LambdaQueryWrapper<SysUserAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserAccount::getUserAccount, account)
                .eq(SysUserAccount::getSysCode, sysCode)
                .eq(SysUserAccount::getStatus, StatusUtils.PASS);
        //查询结果
        SysUserAccount sysUserAccount = sysUserAccountRepository.getOne(queryWrapper);

        return Objects.nonNull(sysUserAccount);

    }

    /**
     * 新增用户账号
     *
     * @param sysUserAccount
     * @return
     */
    public boolean addSysUserAccount(SysUserAccount sysUserAccount) {

        //校验账号是否存在
        if (accountIsExist(sysUserAccount.getUserAccount(), sysUserAccount.getSysCode())) {
            throw new DefaultBusinessException("账号已存在");
        }

        return sysUserAccountRepository.save(sysUserAccount);

    }

    /**
     * 删除用户账号
     *
     * @param userId
     * @return
     */
    public boolean deleteSysUserAccount(Integer userId) {

        //修改条件
        LambdaUpdateWrapper<SysUserAccount> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUserAccount::getUserId, userId);
        updateWrapper.set(SysUserAccount::getStatus, StatusUtils.FAILURE);
        return sysUserAccountRepository.update(updateWrapper);

    }

    /**
     * 修改用户账号
     *
     * @param sysUserAccount
     * @return
     */
    public boolean updateSysUserAccount(SysUserAccount sysUserAccount) {

        //查询条件
        LambdaQueryWrapper<SysUserAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserAccount::getUserId, sysUserAccount.getUserId());
        //查询结果
        SysUserAccount one = sysUserAccountRepository.getOne(queryWrapper);

        if (!one.getUserAccount().equals(sysUserAccount.getUserAccount())) {

            //校验账号是否存在
            if (accountIsExist(sysUserAccount.getUserAccount(), sysUserAccount.getSysCode())) {
                throw new DefaultBusinessException("账号已存在");
            }

        }

        //修改条件
        LambdaUpdateWrapper<SysUserAccount> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUserAccount::getUserId, sysUserAccount.getUserId());
        return sysUserAccountRepository.update(sysUserAccount, updateWrapper);

    }

    /**
     * 账号状态变更
     *
     * @param userId
     * @return
     */
    public boolean change(Integer userId, String status) {
        LambdaUpdateWrapper<SysUserAccount> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUserAccount::getUserId, userId)
                .set(SysUserAccount::getStatus, status);
        return sysUserAccountRepository.update(updateWrapper);
    }

}
